import 'package:homepad/iot/basic_node.dart';
import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/iot/message_def.dart';

class TemperatureSensor extends BasicNode {
  TemperatureSensor.fromJson(super.json) : super.fromJson();

  double _temperature = 26.0;
  double get temperature => _temperature;

  @override
  void readAllState() {
    readAttribute(ClusterIds.temperatureMeasurement, MeasurementAttributeIds.measuredValue);
  }

  @override
  bool onAttributeReport(AttributePathIB path, data) {
    if (path.cluster != ClusterIds.temperatureMeasurement) {
      return super.onAttributeReport(path, data);
    }

    if (path.attribute == MeasurementAttributeIds.measuredValue) {
      var newValue = (data as int) / 100;
      if (newValue == _temperature) {
        return false;
      }
      _temperature = ((newValue + 0.05)* 10).toInt() / 10;
      return true;
    }
    return false;
  }
}
